package com.fgw.admin.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fgw.admin.config.ShiroRealm;
import com.fgw.admin.dto.UserInfo;
import com.fgw.admin.entity.Industry;
import com.fgw.admin.mapper.IndustryMapper;
import com.fgw.admin.service.IIndustryService;
import com.fgw.admin.vo.IndustryVo;


@Service
public class IndustryServiceImpl extends ServiceImpl<IndustryMapper, Industry> implements IIndustryService {
	
	@Autowired
	private IndustryMapper industryMapper;
	
	@Override
	public boolean insert(Industry entity) {
		
		//获取当前用户
		UserInfo userInfo = ShiroRealm.getCurrentUser();
		entity.setCreateTime(new Date());
		entity.setCreateUser(userInfo.getName());
		entity.setCreateUserid(userInfo.getId());
		
		return super.insert(entity);
	}
	
	public List<IndustryVo> getIndustryTree(Integer id){
		
		
		List<IndustryVo> IndustryVoList = getIndustryVo(id);
		
		return IndustryVoList;
	}
	
	
	private IndustryVo getTree(Integer id) {
		
	     IndustryVo industryVo = new IndustryVo();
		
		 Industry industry = industryMapper.selectById(id);
		 
		 industryVo.setId(industry.getIndustryId());
		 industryVo.setTitle(industry.getIndustryName());
		
		 Wrapper<Industry> wrapper = new EntityWrapper<Industry>();
		
		 wrapper.eq("parent_id", id);
		
		 List<Industry> list = industryMapper.selectList(wrapper);
		
		 List<IndustryVo> IndustryVoList = new ArrayList<IndustryVo>();
		 
		 for (int i = 0; i < list.size(); i++) {
			
			 Industry newIndustry = list.get(i);
				
				IndustryVo newIndustryVo = new IndustryVo();
				
				newIndustryVo.setId(newIndustry.getIndustryId());
				newIndustryVo.setTitle(newIndustry.getIndustryName());
				
				IndustryVoList.add(newIndustryVo);
			 
		}
		 industryVo.setChildren(IndustryVoList);
			
		return industryVo;
		
		
	}
	
	
	private List<IndustryVo> getIndustryVo(Integer id){
		
		List<IndustryVo> list = new ArrayList<>();
		
		 Wrapper<Industry> wrapper = new EntityWrapper<Industry>();
			
		 wrapper.eq("parent_id", id);
		
		 List<Industry> industryList = industryMapper.selectList(wrapper);
		 
		 for (int i = 0; i < industryList.size(); i++) {
			
			 Industry industry = industryList.get(i);
			 
			 IndustryVo industryVo = new IndustryVo();
			 
			 industryVo.setId(industry.getIndustryId());
			 industryVo.setTitle(industry.getIndustryName());
			 
			 List<IndustryVo> newIndustryVoList = getIndustryVo(industry.getIndustryId());
			 
			 industryVo.setChildren(newIndustryVoList);
			 
			 list.add(industryVo);
			 
		}
		 
		 return list;
		
	}

}